/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.pconline.autoclub.web.wap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.pconline.autoclub.config.SystemConfig;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.DynaInfo;
import cn.pconline.autoclub.entity.DynaPraise;
import cn.pconline.autoclub.entity.Subject;
import cn.pconline.autoclub.entity.SubjectDyna;
import cn.pconline.autoclub.entity.SubjectPhoto;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.SubjectDynaService;
import cn.pconline.autoclub.service.SubjectService;
import cn.pconline.autoclub.service.UserService;
import cn.pconline.autoclub.service.UserSubjectService;
import cn.pconline.autoclub.util.AppUtils;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.autoclub.web.UserAuth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang.StringUtils;

/**
 *
 * @author xulin
 */
@Controller
@RequestMapping("/wap/subject")
public class SubjectAction {
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private SubjectDynaService subjectDynaService;
    @Autowired
    private UserAuth userAuth;
    @Autowired
    private DynaService dynaService;
    @Autowired
    private UserService userService;
    @Autowired
    private SystemConfig systemConfig;
	@Autowired
	private UserSubjectService userSubjectService;
    private static final int DEF_PAGE_NUM  = 1;
    private static final int DEF_PAGE_SIZE = 10;
    private static final int MAX_PAGE_SIZE = 100;
    private static final int MIN_PAGE_SIZE = 1;
    /**话题列表默认一页记录大小**/
    private static final int LIST_TOPIC_DEF_PAGE_SIZE = 5;
    /**
     * 话题终端页
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/subjectDetail.do",method= RequestMethod.GET)
    public String subjectDetail(HttpServletRequest request,HttpServletResponse response){
        long subjectId = WebUtils.paramLong(request, "subjectId", 0);
        if(subjectId == 0){
            return ActionKit.SNED_ERROR_404;
        }
        Subject subject = subjectService.findSubjectById(subjectId);
        if(subject == null){
            return ActionKit.SNED_ERROR_404;
        }
        List<SubjectPhoto> subjectPhotos = subjectService.listSubjectPhoto(subjectId);
        User user = userAuth.getUser(request, response);
        //过滤导语换行
        subject.setIntroduction(subjectService.filterContent(subject.getIntroduction()));
        request.setAttribute("isLogin",user.getUid()>0);
        request.setAttribute("isSubjectOnwer", subject.getCreateBy() == user.getUid());
        request.setAttribute("subject", subject);
        request.setAttribute("subjectPhotos", subjectPhotos);
        return "template/wap/subject/subjectDetail";
    }
    
    /**
     * 导语详细页
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/subjectIntroduction.do",method= RequestMethod.GET)
    public String subjectIntroduction(HttpServletRequest request,HttpServletResponse response){
        long subjectId = WebUtils.paramLong(request, "subjectId", 0);
        if(subjectId == 0){
            return ActionKit.SNED_ERROR_404;
        }
        Subject subject = subjectService.findSubjectById(subjectId);
        if(subject == null){
            return ActionKit.SNED_ERROR_404;
        }
        //过滤导语换行
        subject.setIntroduction(subjectService.filterContent(subject.getIntroduction()));
        List<SubjectPhoto> subjectPhotos = subjectService.listSubjectPhoto(subjectId);
        request.setAttribute("subjectPhotos", subjectPhotos);
        request.setAttribute("subject", subject);
        return "template/wap/subject/subjectIntroduction";
    }
    
    /**
     * 获取热门话题讨论列表
     * @param request
     * @param response 
     */
    @RequestMapping(value="/listHotSubjectDyna.do",method= RequestMethod.GET)
    public void listHostSubjectDyna(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        long subjectId = WebUtils.paramLong(request, "subjectId", 0);
        int pageNo = WebUtils.paramInt(request, "pageNo", DEF_PAGE_NUM);
        int pageSize = WebUtils.paramInt(request, "pageSize", DEF_PAGE_SIZE,MIN_PAGE_SIZE,MAX_PAGE_SIZE);
        String callback = WebUtils.param(request, "callback", "");
        User user = userAuth.getUser(request, response);
        Pager<DynaInfo> pager = null;
        if(subjectId>0){
             pager = subjectDynaService.pageDynaBySubjectIdOrderReply(subjectId, pageNo, pageSize, Subject.TOP_NO);
        }
        Subject subject = subjectService.findSubjectById(subjectId);
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        if(pager!=null ){
            if(pager.getResultList()!=null && !pager.getResultList().isEmpty()){
                //批量获取点赞列表
                List<DynaPraise> praises = null;
                praises = dynaService.getPraiseList(user.getUid(), pager.getResultList());
                
                for(DynaInfo dynaInfo : pager.getResultList()){
                    JSONObject o = dynaInfo.toJSONObject();
                    o.put("images", dynaService.parseWapImagesJSON(dynaInfo.getImages()));
                    o.put("content", WebUtils.dynaSubjectContent(systemConfig.getWapRoot(),WebUtils.transformToBBSFace(dynaInfo.getContent()), dynaInfo.getSubjectIds()));
                    o.put("faceUrl", WebUtils.face(dynaInfo.getCreateBy(), 100));
                    o.put("niceTime", DateUtils.getFormatedTime(dynaInfo.getCreateAt()));
                    o.put("time", DateUtils.formatDetail(dynaInfo.getCreateAt()));
                    o.put("dynaInfoId", dynaInfo.getDynaInfoId());
                    o.put("createBy", dynaInfo.getCreateBy());
                    o.put("type", Subject.TOP_YES);
                    User author = userService.findUser(dynaInfo.getCreateBy());
                    o.put("isVip", author == null?false:author.getVip() == ClubMember.IS_VIP);
                    o.put("isPraise", false); //是否点过赞
                    o.put("isTop", false);
                    o.put("isSubjectOnwer", false);//是否是该该话题的主人
                    if(subject.getCreateBy() == user.getUid()){
                    	o.put("isSubjectOnwer", true);
                    }
                    if(user.getUid()>0 && praises!=null && !praises.isEmpty()){
                       for(DynaPraise dynaPraise : praises){
                           if(dynaPraise.getDynainfoId() == dynaInfo.getDynaInfoId()){
                               o.put("isPraise", true);
                           }
                       }
                    }
                    array.add(o);
                }
            }
            obj.put("pageNo", pager.getPageNo());
            obj.put("pageSize", pager.getPageSize());
            obj.put("total", pager.getTotal());
            obj.put("pageCount", pager.getPageCount());
        }
        obj.put("data", array);
        
        ActionKit.printSuccessMsg(response, obj,callback);
    }
    
    /**
     * 获取最新话题讨论列表
     * @param request
     * @param response 
     */
    @RequestMapping(value="/listNewSubjectDyna.do",method= RequestMethod.GET)
    public void listNewSubjectDyna(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        long subjectId = WebUtils.paramLong(request, "subjectId", 0);
        int pageNo = WebUtils.paramInt(request, "pageNo", DEF_PAGE_NUM);
        int pageSize = WebUtils.paramInt(request, "pageSize", DEF_PAGE_SIZE,MIN_PAGE_SIZE,MAX_PAGE_SIZE);
        String callback = WebUtils.param(request, "callback", "");
        Pager<DynaInfo> pager = null;
        if(subjectId>0){
             pager = subjectDynaService.pageDynaBySubjectIdOrderNew(subjectId, pageNo, pageSize, Subject.TOP_NO);
        }
        Subject subject = subjectService.findSubjectById(subjectId);
        User user = userAuth.getUser(request, response);
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        if(pager!=null){
            if( pager.getResultList()!=null && !pager.getResultList().isEmpty()){
                //批量获取点赞列表
                List<DynaPraise> praises = null;
                praises = dynaService.getPraiseList(user.getUid(), pager.getResultList());
                
                 for(DynaInfo dynaInfo : pager.getResultList()){
                    JSONObject o = dynaInfo.toJSONObject();
                    o.put("images", dynaService.parseWapImagesJSON(dynaInfo.getImages()));
                    o.put("content", WebUtils.dynaSubjectContent(systemConfig.getWapRoot(),WebUtils.transformToBBSFace(dynaInfo.getContent()), dynaInfo.getSubjectIds()));
                    o.put("faceUrl", WebUtils.face(dynaInfo.getCreateBy(), 100));
                    o.put("niceTime", DateUtils.getFormatedTime(dynaInfo.getCreateAt()));
                    o.put("time", DateUtils.formatDetail(dynaInfo.getCreateAt()));
                    o.put("dynaInfoId", dynaInfo.getDynaInfoId());
                    o.put("createBy", dynaInfo.getCreateBy());
                    o.put("type", Subject.TOP_YES);
                    User author = userService.findUser(dynaInfo.getCreateBy());
                    o.put("isVip", author == null?false:author.getVip() == ClubMember.IS_VIP);
                    o.put("isPraise", false); //是否点过赞
                    o.put("isTop", false);
                    o.put("isSubjectOnwer", false);//是否是该该话题的主人
                    if(subject.getCreateBy() == user.getUid()){
                    	o.put("isSubjectOnwer", true);
                    }
                    if(user.getUid()>0 && praises!=null && !praises.isEmpty()){
                        for(DynaPraise dynaPraise : praises){
                           if(dynaPraise.getDynainfoId() == dynaInfo.getDynaInfoId()){
                               o.put("isPraise", true);
                           }
                       }
                    }
                    array.add(o);
                }
            obj.put("pageNo", pager.getPageNo());
            obj.put("pageSize", pager.getPageSize());
            obj.put("total", pager.getTotal());
            obj.put("pageCount", pager.getPageCount());
        }
        }
        obj.put("data", array);
        ActionKit.printSuccessMsg(response, obj,callback);
    }
    
    
    /**
     * 获取话题置顶讨论列表
     * @param request
     * @param response 
     */
    @RequestMapping(value="/listSubjectTopDyna.do",method= RequestMethod.GET)
    public void listSubjectTopDyna(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        long subjectId = WebUtils.paramLong(request, "subjectId", 0);
        String callback = WebUtils.param(request, "callback", "");
        User user = userAuth.getUser(request, response);
        List<DynaInfo> dynaList = null;
        if(subjectId>0){
        	dynaList = subjectDynaService.listDynaByTop(subjectId);
        }
        Subject subject = subjectService.findSubjectById(subjectId);
        JSONObject obj = new JSONObject();
        JSONArray array = new JSONArray();
        if(dynaList!=null && !dynaList.isEmpty() ){
               //批量获取点赞列表
                List<DynaPraise> praises = null;
                praises = dynaService.getPraiseList(user.getUid(), dynaList);
                
                for(DynaInfo dynaInfo : dynaList){
                    JSONObject o = dynaInfo.toJSONObject();
                    o.put("images", dynaService.parseWapImagesJSON(dynaInfo.getImages()));
                    o.put("content", WebUtils.dynaSubjectContent(systemConfig.getWapRoot(),WebUtils.transformToBBSFace(dynaInfo.getContent()), dynaInfo.getSubjectIds()));
                    o.put("faceUrl", WebUtils.face(dynaInfo.getCreateBy(), 100));
                    o.put("niceTime", DateUtils.getFormatedTime(dynaInfo.getCreateAt()));
                    o.put("time", DateUtils.formatDetail(dynaInfo.getCreateAt()));
                    o.put("dynaInfoId", dynaInfo.getDynaInfoId());
                    o.put("createBy", dynaInfo.getCreateBy());
                    o.put("type", Subject.TOP_NO);
                    User author = userService.findUser(dynaInfo.getCreateBy());
                    o.put("isVip", author == null?false:author.getVip() == ClubMember.IS_VIP);
                    o.put("isPraise", false); //是否点过赞
                    o.put("isTop", true);
                    o.put("isSubjectOnwer", false);//是否是该该话题的主人
                    if(subject.getCreateBy() == user.getUid()){
                    	o.put("isSubjectOnwer", true);
                    }
                    if(user.getUid()>0 && praises!=null && !praises.isEmpty()){
                        for(DynaPraise dynaPraise : praises){
                           if(dynaPraise.getDynainfoId() == dynaInfo.getDynaInfoId()){
                               o.put("isPraise", true);
                           }
                       }
                    }
                    array.add(o);
                }
        }
        obj.put("data", array);
        
        ActionKit.printSuccessMsg(response, obj,callback);
    }
    /**
     * PV +1
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/addPV.do", method = RequestMethod.POST)
    public void addPV(HttpServletRequest request, HttpServletResponse response){
    	WebUtils.setEncoding(request, response);
    	long subjectId = WebUtils.paramLong( request, "subjectId", 0);
        Subject subject = subjectService.findSubjectById(subjectId);
        if(subject==null){
        	AppUtils.printErrorMsg(response,"操作失败，话题不存在", "");
        	return;
        }
        long count = subject.getPvCount()+1;
        subject.setPvCount(count);
        int result =subjectService.updateByField( subject, "pv_count");
        if(result>0){
        	AppUtils.printSuccessMsg( response, "更新成功", null, "" );
        	return;
        }
        AppUtils.printErrorMsg(response,"更新失败", "");
    }
    public void listClubByBrand(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        
    }
    
    /**
     * 话题列表页(全部话题)
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/subjectList.do",method= RequestMethod.GET)
    public String subjectList(HttpServletRequest request,HttpServletResponse response){
        //全部话题
    	Pager<Subject> pager = subjectService.pageSubjectByHot(DEF_PAGE_NUM,LIST_TOPIC_DEF_PAGE_SIZE);
    	for(int i=0; i< pager.getResultList().size(); i++){
            Subject subject =  pager.getResultList().get(i);
            pager.getResultList().get(i).setCreateName(userService.getNickNameByUserId(subject.getCreateBy()));
        }
        
        //本周话题
        Date beginDate = DateUtils.getSingleDateBefore(new Date(), 7);
        Pager<Subject> pager2 = subjectService.pageSubjectByHot(DEF_PAGE_NUM,LIST_TOPIC_DEF_PAGE_SIZE,beginDate);
      
        //如果本周不够5条话题，会补充本周以外最新时间的话题，按照PV排序
        List<Subject> subjects = new ArrayList<Subject>();
        Pager<Subject> pager3 = null;
        if(pager2.getResultList().size()<LIST_TOPIC_DEF_PAGE_SIZE){
        	pager3 = subjectService.pageSubjectByNew(DEF_PAGE_NUM, LIST_TOPIC_DEF_PAGE_SIZE);
        	for(Subject subject : pager3.getResultList()){
        		Boolean flag = false;
        		for(Subject subject2 : pager2.getResultList()){
        			if(subject.getSubjectId() == subject2.getSubjectId()){
        				flag =true;
        				break;
        			}
        		}
        		if(!flag){
        			subjects.add(subject);
        		}
        	}
        	//按pv排序
        	Collections.sort(subjects, new Comparator<Subject>() {
                @Override
                public int compare(Subject s1, Subject s2) {
                    return (int) (s2.getPvCount() - s1.getPvCount());
                }
            });
        	for(int i=0; i< subjects.size(); i++){
                 Subject subject =  subjects.get(i);
                 subjects.get(i).setCreateName(userService.getNickNameByUserId(subject.getCreateBy()));      
            }
        }
        for(int i=0; i< pager2.getResultList().size(); i++){
            Subject subject =  pager2.getResultList().get(i);
            pager2.getResultList().get(i).setCreateName(userService.getNickNameByUserId(subject.getCreateBy()));      
        }
        request.setAttribute("pager", pager);
        request.setAttribute("pager2", pager2);
        request.setAttribute("subjects", subjects);
        return "template/wap/subject/subjectList";
    }
    
    
    /**
     * 查询热门话题
     * @param request
     * @param response 
     */
    @RequestMapping(value="/listHotSubject.do",method= RequestMethod.GET)
    public void listHotSubject(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        int pageNo = WebUtils.paramInt(request, "pageNo", DEF_PAGE_NUM);
        int pageSize = WebUtils.paramInt(request, "pageSize", LIST_TOPIC_DEF_PAGE_SIZE);
        String callback = WebUtils.param(request, "callback", "");
        Pager<Subject> pager = subjectService.pageSubjectByHot(pageNo,pageSize);
        JSONArray array = new JSONArray();
        JSONObject obj = new JSONObject();
        if(pager != null){
            if(pager.getResultList()!=null && !pager.getResultList().isEmpty()){
            	int rank = (pageNo-1)*pageSize;
                for(Subject subject : pager.getResultList()){
                    JSONObject o = new JSONObject();
                    o.put("subjectId", subject.getSubjectId());
                    o.put("title", EncodeUtils.encodeForHTML(subject.getTitle(),true));
                    o.put("pvCount", subject.getPvCount());
                    o.put("replyCount", subject.getReplyCount());
                    String createName = userService.getNickNameByUserId(subject.getCreateBy());
                    o.put("createName", EncodeUtils.encodeForHTML(createName,true));
                    o.put("logo", subject.getLogo()!=null ? subject.getLogo():subject.DEFAULT_LOGO);
                    o.put("rank", ++rank);
                    array.add(o);
                }
            }
            obj.put("pageNo", pager.getPageNo());
            obj.put("pageSize", pager.getPageSize());
            obj.put("total", pager.getTotal());
            obj.put("pageCount", pager.getPageCount());
        }
        obj.put("data", array);
        ActionKit.printSuccessMsg(response, obj, callback);
    }
      
    /**
     * 查询本周热门话题
     * @param request
     * @param response 
     */
    @RequestMapping(value="/listCurrentWeekSubject.do",method= RequestMethod.GET)
    public void listCurrentWeekSubject(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        int pageNo = WebUtils.paramInt(request, "pageNo", DEF_PAGE_NUM);
        int pageSize = WebUtils.paramInt(request, "pageSize", LIST_TOPIC_DEF_PAGE_SIZE);
        String callback = WebUtils.param(request, "callback", "");
        Date beginDate = DateUtils.getSingleDateBefore(new Date(), 7);//获得七天前的时间
        Pager<Subject> pager = subjectService.pageSubjectByHot(pageNo,pageSize,beginDate);
        JSONArray array = new JSONArray();
        JSONObject obj = new JSONObject();
        if(pager != null){
            if(pager.getResultList()!=null && !pager.getResultList().isEmpty()){
            	int rank = (pageNo-1)*pageSize;
                for(Subject subject : pager.getResultList()){
                    JSONObject o = new JSONObject();
                    o.put("subjectId", subject.getSubjectId());
                    o.put("title", EncodeUtils.encodeForHTML(subject.getTitle(),true));
                    o.put("pvCount", subject.getPvCount());
                    o.put("replyCount", subject.getReplyCount());
                    String createName = userService.getNickNameByUserId(subject.getCreateBy());
                    o.put("createName", EncodeUtils.encodeForHTML(createName,true));
                    o.put("logo", subject.getLogo()!=null ? subject.getLogo():subject.DEFAULT_LOGO);
                    o.put("rank", ++rank);
                    array.add(o);
                }
            }
            obj.put("pageNo", pager.getPageNo());
            obj.put("pageSize", pager.getPageSize());
            obj.put("total", pager.getTotal());
            obj.put("pageCount", pager.getPageCount());
        }
        obj.put("data", array);
        ActionKit.printSuccessMsg(response, obj, callback);
    }
    /*
     * 编辑导语页面
     */
    @RequestMapping(value="/updateIntroductionPage.do",method= RequestMethod.GET)
    public String updateIntroductionPage(HttpServletRequest request,HttpServletResponse response){
        long subjectId = WebUtils.paramLong(request, "subjectId", 0);
        if( subjectId <= 0 ){
            return ActionKit.SNED_ERROR_404;
        }
        Subject subject = subjectService.findSubjectById(subjectId);
        if(subject  == null){
            return ActionKit.SNED_ERROR_404;
        }
        User user = userAuth.getUser(request, response);
        if(user.getUid()==0){
            String returnUrl = systemConfig.getWapRoot()+"/topic/"+subjectId+"/edit/";
            ActionKit.sendLogin(systemConfig.getUcWapRoot()+"/passport/login.jsp?return="+returnUrl, response);
            return null;
        }
        long supperMan = systemConfig.getSubjectSupperMan();
        if(user.getUid()!=subject.getCreateBy() && user.getUid() != supperMan) {
            return ActionKit.SNED_ERROR_404;
        }
        if(subject.hasPic()){
            List<SubjectPhoto> subjectPhoto = subjectService.listSubjectPhoto(subjectId);
            request.setAttribute("subjectPhoto", subjectPhoto);
        }
        String intr  = subject.getIntroduction();
        if(!StringUtils.isBlank(intr)){
            subject.setIntroduction(intr.replace("<br>", "\r\n"));
        }
        request.setAttribute("subject", subject);
        request.setAttribute("user", user);
        return "/template/wap/subject/subject";
    }
    /**
   	 * 删除话题动态
   	 * @param request
   	 * @param response
   	 */
   	@ResponseBody
       @RequestMapping(value = "/deleteSubjectDyna.do", method = RequestMethod.POST)
   	public void deleteSubjectDyna(HttpServletRequest request, HttpServletResponse response){
   		WebUtils.setEncoding(request, response);
   		String callback = WebUtils.param(request, "callback", "");
   		long subjectId = WebUtils.paramLong( request, "subjectId", 0);
   		long dynaInfoId = WebUtils.paramLong( request, "dynaInfoId", 0);
   		long createBy = WebUtils.paramLong( request, "createBy", 0);
   		Subject subject = subjectService.findSubjectById(subjectId);
   		if(subject==null){
   			AppUtils.printErrorMsg(response,"操作失败，话题不存在", callback);
           	return;
           }
   		User user = userAuth.getUser(request, response);//验证用户是否登录
           if(user.getUid()!=subject.getCreateBy()){
           	AppUtils.printErrorMsg(response,"操作失败，没有权限", callback);
           	return;
           }
           SubjectDyna subjectDyna = subjectDynaService.findSubjectDynaById(subjectId, dynaInfoId);
           if(subjectDyna != null){
           	   //删除话题动态同时减去话题相应的讨论数    话题的讨论数 = 话题动态数+话题动态回复数 
           	  subject.setReplyCount(subject.getReplyCount()-subjectDyna.getPostCount());
           }
           subjectDynaService.deleteSubjectDyna( subjectId, dynaInfoId );
           userSubjectService.deleteUserSubject( createBy, subjectId, dynaInfoId );
           subject.setReplyCount(subject.getReplyCount()-1);
           subjectService.updateByField( subject, "reply_count" );
           AppUtils.printSuccessMsg( response, "删除成功", null, callback );
   		
   	}
   	
   	/**
   	 * 置顶（取消置顶）
   	 * @param request
   	 * @param response
   	 */
   	@ResponseBody
       @RequestMapping(value = "/setTop.do", method = RequestMethod.POST)
   	public void setTop(HttpServletRequest request, HttpServletResponse response){
   		WebUtils.setEncoding(request, response);
   		String callback = WebUtils.param(request, "callback", "");
   		long subjectId = WebUtils.paramLong( request, "subjectId", 0);
   		long dynaInfoId = WebUtils.paramLong( request, "dynaInfoId", 0);
   		long createBy = WebUtils.paramLong( request, "createBy", 0);
   		int type = WebUtils.paramInt( request, "type", Subject.TOP_NO);
   		Subject subject = subjectService.findSubjectById(subjectId);
   		if(subject==null){
   			AppUtils.printErrorMsg(response,"操作失败，话题不存在", callback);
           	return;
           }
   		User user = userAuth.getUser(request, response);//验证用户是否登录
           if(user.getUid()!=subject.getCreateBy()){
           	AppUtils.printErrorMsg(response,"操作失败，没有权限", callback);
           	return;
           }
           subjectDynaService.updateTop( subjectId, dynaInfoId, type );
           userSubjectService.updateTop( createBy, subjectId, dynaInfoId, type );
           AppUtils.printSuccessMsg( response, "操作成功", null, callback );
   	}
}
